package com.hilihili.service.impl;

import com.hilihili.domain.entity.Favorites;
import com.hilihili.mapper.FavoritesMapper;
import com.hilihili.service.IFavoritesService;
import com.hilihili.util.PictureMD5Util;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class FavoritesServiceImpl implements IFavoritesService
{
    @Autowired
    private FavoritesMapper favoritesMapper;


    @Override
    public List<Favorites> selectFavoritesList(Long createAt) {
        return favoritesMapper.selectFavoritesList(createAt);
    }

    @Override
    public Boolean updateFavorites(Favorites favorites,String uploadPath) {
        if (favorites.getFavoritesCover() != null && !"".equals(favorites.getFavoritesCover())){
            // 删除原来的图片
            File file = new File(uploadPath + favorites.getFavoritesCover());
            boolean delete = file.delete();
            // 生成新的图片
            String newFileName = UUID.randomUUID().toString() + ".png";
            String newPath = uploadPath + SecurityUtils.getUserId() + "\\" + "favoritesCover\\";
            File dirs = new File(newPath);
            //判断当前目录是否存在：目录不存在，需要创建
            if(!dirs.exists()) dirs.mkdirs();
            Boolean aBoolean1 = PictureMD5Util.generatePicture(favorites.getImgBase64(), newPath + newFileName);
            if (!aBoolean1){
                return false;
            }
            favorites.setFavoritesCover(SecurityUtils.getUserId() + "\\" + "favoritesCover\\" + newFileName);
            favoritesMapper.updateFavorites(favorites);
            return true;
        }else {
            if (favorites.getImgBase64() != null) {
                // 初次修改图片// 生成新的图片
                String newFileName = UUID.randomUUID().toString() + ".png";
                String newPath = uploadPath + SecurityUtils.getUserId() + "\\" + "favoritesCover\\";
                File dirs = new File(newPath);
                //判断当前目录是否存在：目录不存在，需要创建
                if (!dirs.exists()) dirs.mkdirs();
                Boolean aBoolean1 = PictureMD5Util.generatePicture(favorites.getImgBase64(), newPath + newFileName);
                if (!aBoolean1) {
                    return false;
                }
                favorites.setFavoritesCover(SecurityUtils.getUserId() + "\\" + "favoritesCover\\" + newFileName);
                favoritesMapper.updateFavorites(favorites);
                return true;
            }else {
                favoritesMapper.updateFavorites(favorites);
                return true;
            }
        }
    }

    @Override
    public int saveFavorites(Favorites favorites) {
        return favoritesMapper.saveFavorites(favorites);
    }

    @Override
    public int deleteFavorites(Long id) {
        return favoritesMapper.deleteFavorites(id);
    }

    @Override
    public void updateFavoritesRank(String[] ids) {
        List<Favorites> favoritesList = new ArrayList<>();
        for (int i = 1; i < ids.length; i++) {
            Favorites favorites = new Favorites();
            favorites.setFavoritesRank(i+1);
            favorites.setId(Long.parseLong(ids[i]));
            favoritesList.add(favorites);
        }
        favoritesMapper.batchUpdateFavoritesRank(favoritesList);
    }

    @Override
    public List<Favorites> selectDefaultFavorites(Long createAt) {
        return favoritesMapper.selectDefaultFavorites(createAt);
    }
}
